//
//  AnnouncementController.m
//  Village
//
//  Created by 程虎 on 2017/9/13.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "AnnouncementController.h"
#import "AnnouncementsCell.h"
#import "TZGGHttpTools.h"
#import "AnnounModel.h"

@interface AnnouncementController ()
@property (nonatomic, strong) NSMutableArray *dataAry;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageCount;//总页数
@end

@implementation AnnouncementController

- (void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通知公告";
    
    [self config_UI];
}

- (void)config_UI
{
    self.pageNum = 0;
    self.pageCount = 0;
    [self.tabView.mj_header beginRefreshing];
    [self.tabView registerNib:[UINib nibWithNibName:@"AnnouncementsCell" bundle:nil] forCellReuseIdentifier:@"AnnouncementsCell"];
}

#pragma mark - 下拉刷新
- (void)refreshheader_data
{
    self.pageNum  = 1;
    self.pageCount = 0;
    [self.tabView.mj_footer setState:MJRefreshStateIdle];
    [self getannucements:self.pageNum];
}

#pragma amrk - 下拉刷新
- (void)refreshfoot_data
{
    self.pageNum += 1;
    if (self.pageNum < self.pageCount + 1) {
        [self getannucements:self.pageNum];
    }
    else {
        [EmptyFunction hideEmpty:self.tabView];
        [self.tabView.mj_footer endRefreshingWithNoMoreData];
    }
}

#pragma mark - tableviewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AnnouncementsCell *cell = [AnnouncementsCell cellWithTableView:tableView];
    cell.model = self.dataAry[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self changeTZGGReadStatus:self.dataAry[indexPath.row]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Class currentClass = [AnnouncementsCell class];
    return [self.tabView cellHeightForIndexPath:indexPath model:self.dataAry[indexPath.row] keyPath:@"model" cellClass:currentClass contentViewWidth:[self cellContentViewWith]] ;
}

- (CGFloat)cellContentViewWith
{
    //contentViewWidth  一定要计算cell的宽度  计算出来的高度 才是准确的
    CGFloat width = SCREEN_W ;
    
    // 适配ios7横屏
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}

#pragma mark - 查询通知公告
- (void)getannucements:(NSInteger )index
{
    [MBProgressHUD showMessage:@"正在查询..." toView:self.navigationController.view];
    WeakSelf(weakSelf);
    [TZGGHttpTools GetTZGGPageNum:index Success:^(NSDictionary *valueDict) {
        [MBProgressHUD hideHUDForView:weakSelf.navigationController.view];
        if (index == 1) {
            [weakSelf.tabView.mj_header endRefreshing];
            self.pageCount = [valueDict[@"pagecount"] integerValue];
            [self config_data:[AnnounModel mj_objectArrayWithKeyValuesArray:valueDict[@"list"]]];
        }
        else {
            [weakSelf.tabView.mj_footer endRefreshing];
            NSMutableArray *array = [NSMutableArray arrayWithArray:self.dataAry];
            [array addObjectsFromArray:[AnnounModel mj_objectArrayWithKeyValuesArray:valueDict[@"list"]]];
            [self config_data:array];
        }
        [EmptyFunction hideEmpty:self.tabView];
//        [weakSelf.tabView reloadData];
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUDForView:weakSelf.navigationController.view];
        if (index == 1) {
            [weakSelf.tabView.mj_header endRefreshing];
        }
        else {
            [weakSelf.tabView.mj_footer endRefreshing];
        }
        [EmptyFunction showEmpty:self.tabView Height:self.tabView.frame.size.height];
    }];
}

#pragma mark - 更新阅读状态
- (void)changeTZGGReadStatus:(AnnounModel *)model
{
    [MBProgressHUD showMessage:@"正在加载..." toView:self.navigationController.view];
    WeakSelf(weakSelf);
    [TZGGHttpTools AddTZGGReadingStatusWithArticlesID:model.newsid Success:^() {
        [MBProgressHUD hideHUDForView:weakSelf.navigationController.view];
        [Tools ToNextName:@"InformDetailsController" Params:model CurrentVC:self Completion:^(id obj) {
            [weakSelf getannucements:1];
        }];
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUDForView:weakSelf.navigationController.view];
        [MBProgressHUD showError:error toView:weakSelf.navigationController.view];
    }];
}
#pragma  mark - 配置数据
- (void)config_data:(NSArray *)arrays
{ 
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSMutableArray *noreadAry = [NSMutableArray array];
        NSMutableArray *readAry = [NSMutableArray array];
        for (int i = 0; i < arrays.count; i++) {
            AnnounModel *model = arrays[i];
            if ([model.isread isEqualToString:@"0"]) {
                [noreadAry addObject:model];
            }
            else {
                [readAry addObject:model];
            }
        }
        self.dataAry = [NSMutableArray array];
        [self.dataAry addObjectsFromArray:noreadAry];
        [self.dataAry addObjectsFromArray:readAry];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tabView reloadData];
        });
    });
}
@end
